import {
	type IRequest,
	type IRequestForm,
	type IRequests,
	requestScheme,
	requestsScheme
} from '@/types/request.types'

import { axiosWithAuth } from '@/api/interceptors'

export const requestService = {
	async fetchMany(ids: number[] = []) {
		const response = await axiosWithAuth.get<IRequests>('/requests/', {
			params: { ids: ids.join(',') }
		})

		const requests = requestsScheme.safeParse(response.data)
		console.log(response.data, requests)

		return requestsScheme.parse(response.data)
	},
	async fetch(id: string) {
		const response = await axiosWithAuth.get<IRequest>(`/requests/${id}/`)

		return requestScheme.parse(response.data)
	},
	async update(id: number, data: IRequestForm) {
		const response = await axiosWithAuth.put<IRequest>(`/requests/${id}/`, data)

		return requestScheme.parse(response.data)
	},
	async create(data: IRequestForm) {
		const response = await axiosWithAuth.post<IRequest>('/requests/', data)

		return requestScheme.parse(response.data)
	},
	async del(id: number) {
		const response = await axiosWithAuth.delete(`/requests/${id}/`)
		// console.log(response)

		return response.status === 204
	}
}
